package ferp.core.ai.filter;

import ferp.core.card.Card;
import ferp.core.player.Hand;

/**
 * User: olegs
 * Date: 1/21/13 6:48 PM
 */
public class FilterWhisterFirstHandMove
{
  // we should get here only if a player is whister or passer during tricking game
  public static int apply(int candidates, int playerCards, int declarerCards)
  {
    int filtered = candidates;

    for (Card.Suit s : Card.Suit.rising)
      if (Hand.only(candidates, s) != 0)
      {
        int cards = Hand.get(declarerCards, s);
        int cardsLength = Card.Suit.length(cards);

        int whisterCards = Hand.get(playerCards, s);
        int whisterCardsLength = Card.Suit.length(whisterCards);

        //Resolver.debug(true);

        // second+ king situation (without queen)
        boolean ace = Hand.contains(cards, Card.Mask.ACE);

        boolean secondKing = ace && cardsLength > 1 && Hand.contains(whisterCards, Card.Mask.KING) &&
                                                       !Hand.contains(whisterCards, Card.Mask.QUEEN) &&
                                                       whisterCardsLength > 1 && whisterCardsLength < 4;

        // third+ queen situation
        boolean king = Hand.contains(cards, Card.Mask.KING);

        boolean thirdQueen = Hand.contains(whisterCards, Card.Mask.QUEEN) &&
                             ((ace && king && cardsLength > 2 && whisterCardsLength > 2) ||
                              (ace && !king && cardsLength > 1 && whisterCardsLength > 1) ||
                              (!ace && king && cardsLength > 1 && whisterCardsLength > 1));

        // fourth jack situation
        boolean queen = Hand.contains(cards, Card.Mask.QUEEN);

        boolean fourthJack = ace && king && queen && cardsLength > 3 && Hand.contains(whisterCards, Card.Mask.JACK) && whisterCardsLength > 3;

        // in one of the below situations remove the entire suit for the candidates
        if (secondKing || thirdQueen || fourthJack)
          filtered = Hand.remove(filtered, s);
      }

    if (filtered == 0)
      return candidates;
    return filtered;
  }
}
